package org.boot.ssm.wxshop.web.controller;

import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.service.UserService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @program: wxshop
 * @description: 登陆的控制器
 * @author: Parasomnia
 * @create: 2018-03-12 10:09
 **/

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * Todo:直接进入后台管理，用于后台代码编写，请用于在登录模块完成后删除！！
     *
     * @return 后台管理界面
     */
    @RequestMapping("/mgr")
    public String toMgr() {
        //加拦截器
        return "backstage/panel/index";
    }

    /*****************************
     * @Description: 进入前台登陆页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/18
     *******************************/
    @RequestMapping("/frontstage/login")
    public String frontStageLogin() {
        return "frontstage/panel/login";
    }

    @RequestMapping("/login")
    /*****************************
     * @Description: 进入后台登陆页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/18
     *******************************/
    public String login() {
        return "backstage/panel/login";
    }


    @RequestMapping(value = "/doLogin")
    @ResponseBody
    /*****************************
     * @Description: 登陆验证
     * @Param: [session, user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/18
     *******************************/
    public JsonResult doLogin(HttpSession session, User user) {
        user.setIsAdmin(1);
        JsonResult jsonResult = userService.backstageLogin(user);
        session.setAttribute("theUser", jsonResult.getData());
        return jsonResult;
    }

    @RequestMapping(value = "/frontstage/doLogin")
    @ResponseBody
    /*****************************
     * @Description: 前台登陆验证
     * @Param: [session, user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/19
     *******************************/
    public JsonResult doFrontStageLogin(HttpSession session, User user) {
        user.setIsAdmin(0);
        JsonResult jsonResult = userService.backstageLogin(user);
        session.setAttribute("user", jsonResult.getData());
        return jsonResult;
    }


    @RequestMapping(value = "/loginExit")
    /*****************************
    * @Description: 用户退出 
    * @Param: [session] 
    * @return: java.lang.String 
    * @Author: 刘涛 
    * @Date: 2018/3/18 
    *******************************/
    public String loginExit(HttpSession session) {
        session.removeAttribute("theUser");
        return "backstage/panel/login";
    }


    /*****************************
     * @Description: 前台用户退出
     * @Param: [session]
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/23
     *******************************/
    @RequestMapping(value = "/frontstage/loginExit")
    public String frontstageLoginExit(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/index";
    }

}
